﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLLottery.Common;
using System.ComponentModel;

namespace SLLottery.Controls
{
    public partial class LotteryBallContainer : UserControl
    {
        public BallSize size
        {
            get;
            set;
        }
        public string LotteryNum
        {
            get { return (string)base.GetValue(LotteryNumProperty); }

            set { base.SetValue(LotteryNumProperty, value); }
        }
        public static readonly DependencyProperty LotteryNumProperty = DependencyProperty.Register("LotteryNum", typeof(string), typeof(LotteryBallContainer),
               new PropertyMetadata(null, new PropertyChangedCallback(LotteryBallContainer.OnLotteryNumPropertyChanged)));

        public LotteryBallContainer()
        {
            InitializeComponent();
        }
        private static void OnLotteryNumPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

            (d as LotteryBallContainer).CreateNumsBall(e.NewValue as string);
        }
        void CreateNumsBall(string value)
        {
            BallContainer.Children.Clear();
            int ballCount = value.Length;
            if (value.IndexOf(",") > -1)
            {
                string[] array = value.Split(',');
                ballCount = array.Length;
            }
            for (int i = 0; i < ballCount; i++)
            {
                LotteryBall ball = new LotteryBall();
                if (size == BallSize.Big)
                {
                    
                    if (ballCount == 7
                        && i == ballCount - 1)
                        ball.BallColor = BallColor.Blue_Big;
                    else
                        ball.BallColor = BallColor.Red_Big;
                }
                else
                {
                    if (ballCount == 7
                       && i == ballCount - 1)
                        ball.BallColor = BallColor.Blue_Small;
                    else
                        ball.BallColor = BallColor.Red_Small;
                }
                if(size==BallSize.Big)
                    ball.Margin = new Thickness(2);
                if (value.IndexOf(",") > -1)
                {
                    string[] array = value.Split(',');
                    ball.tb.Text = array[i];
                }
                else
                    ball.tb.Text = value.Substring(i, 1);
                BallContainer.Children.Add(ball);
            }
        }
       
    }
    public enum BallSize
    {
        Big,
        Small
    }
}
